/////////////////////////////////////////////////////////////////////////////
// Name:        slposapp.cpp
// Purpose:     
// Author:      Herry Sugiharto
// Modified by: 
// Created:     07/09/2008 00:21:20
// RCS-ID:      
// Copyright:   Copyright (c) 2000, 2008 Krida Software Division
// Licence:     
/////////////////////////////////////////////////////////////////////////////

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

////@begin includes
////@end includes

#include "slposapp.h"

////@begin XPM images
////@end XPM images


/*!
 * Application instance implementation
 */

////@begin implement app
IMPLEMENT_APP( SLposApp )
////@end implement app


/*!
 * SLposApp type definition
 */

IMPLEMENT_CLASS( SLposApp, wxApp )


/*!
 * SLposApp event table definition
 */

BEGIN_EVENT_TABLE( SLposApp, wxApp )

////@begin SLposApp event table entries
////@end SLposApp event table entries

END_EVENT_TABLE()


/*!
 * Constructor for SLposApp
 */

SLposApp::SLposApp()
{
    Init();
}


/*!
 * Member initialisation
 */

void SLposApp::Init()
{
////@begin SLposApp member initialisation
////@end SLposApp member initialisation
    config = new wxConfig(wxT("slpos"));
    
    ServerAddress   = config->Read(wxT("ServerAddress"), wxT("127.0.0.1"));
    ServerPort      = config->Read(wxT("ServerPort"), wxT("5432"));
    DatabaseName    = config->Read(wxT("DatabaseName"), wxT(""));
    gUsername       = config->Read(wxT("DBlogin"), wxT(""));
    gPassword       = config->Read(wxT("DBpass"), wxT(""));
    
    DefaultCustomer = config->Read(wxT("Customer"));
    CashAccount     = config->Read(wxT("CashAccount"));
    CardAccount     = config->Read(wxT("CardAccount"));
    ARaccount       = config->Read(wxT("ARaccount"));
    
    MarginTop       = config->Read(wxT("MarginTop"), wxT("0"));
    MarginLeft      = config->Read(wxT("MarginLeft"), wxT("0"));
    
    CompanyName     = config->Read(wxT("CompanyName"));
    CompanyAddress  = config->Read(wxT("CompanyAddress"));
    CompanyPhone    = config->Read(wxT("CompanyPhone"));
    PrintFooter     = config->Read(wxT("PrintFooter"), _("Thank you for your valued business"));

}

/*!
 * Initialisation for SLposApp
 */

bool SLposApp::OnInit()
{    

#if wxUSE_XPM
	wxImage::AddHandler(new wxXPMHandler);
#endif
#if wxUSE_LIBPNG
	wxImage::AddHandler(new wxPNGHandler);
#endif
#if wxUSE_LIBJPEG
	wxImage::AddHandler(new wxJPEGHandler);
#endif
#if wxUSE_GIF
	wxImage::AddHandler(new wxGIFHandler);
#endif

    // ask user for username and password
/*    gUsername = wxGetTextFromUser(_("Username:"), _("Cashier Login"));
    if (gUsername.IsEmpty()) return false;
    gPassword = wxGetPasswordFromUser(_("Password"), _("Cashier Login"));
    if (gPassword.IsEmpty()) return false;
*/
    // connect to database
    conninfo = wxT("hostaddr=") + ServerAddress;
    conninfo += wxT(" port=") + ServerPort;
    conninfo += wxT(" dbname=") + DatabaseName;
    conninfo += wxT(" user=") + gUsername;
    conninfo += wxT(" password=") + gPassword;
    
    // Make a connection to the database
    conn = PQconnectdb(conninfo.char_str());
    
    // Check to see that the backend connection was successfully made
    if (PQstatus(conn) != CONNECTION_OK)
    {
        wxLogError(_("Unable to connect to server"));
        wxLogError(PQerrorMessage(conn));
        return false;
    }
    
    
	SLPointofSale* mainWindow = new SLPointofSale(NULL);
	/* int returnValue = */ mainWindow->ShowModal();

	mainWindow->Destroy();
	
    // close the connection to database
    PQfinish(conn);
    
    // write back all the setting to config file
    config->Write("ServerAddress", ServerAddress);
    config->Write("ServerPort", ServerPort);
    config->Write("DatabaseName", DatabaseName);
    config->Write("DBlogin", gUsername);
    config->Write("DBpass", gPassword);
    
    config->Write("Customer", DefaultCustomer);
    config->Write("CashAccount", CashAccount);
    config->Write("CardAccount", CardAccount);
    config->Write("ARaccount", ARaccount);
    config->Write("MarginTop", MarginTop);
    config->Write("MarginLeft", MarginLeft);

    config->Write("CompanyName", CompanyName);
    config->Write("CompanyAddress", CompanyAddress);
    config->Write("CompanyPhone", CompanyPhone);
    config->Write("PrintFooter", PrintFooter);

    delete config;
    delete printer;
    
    // A modal dialog application should return false to terminate the app.
	return false;
    
}


/*!
 * Cleanup for SLposApp
 */

int SLposApp::OnExit()
{   
    // close the connection to database
    PQfinish(conn);
    
    // write back all the setting to config file
    config->Write("ServerAddress", ServerAddress);
    config->Write("ServerPort", ServerPort);
    config->Write("DatabaseName", DatabaseName);
    config->Write("DBlogin", gUsername);
    config->Write("DBpass", gPassword);
    
    config->Write("Customer", DefaultCustomer);
    config->Write("CashAccount", CashAccount);
    config->Write("CardAccount", CardAccount);
    config->Write("ARaccount", ARaccount);
    
    config->Write("MarginTop", MarginTop);
    config->Write("MarginLeft", MarginLeft);
    config->Write("PrintFooter", PrintFooter);

    config->Write("CompanyName", CompanyName);
    config->Write("CompanyAddress", CompanyAddress);
    config->Write("CompanyPhone", CompanyPhone);

    delete config;
    delete printer;

////@begin SLposApp cleanup
	return wxApp::OnExit();
////@end SLposApp cleanup
}

